<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>Tune for disk usage | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'tune-for-disk-usage.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/tune-for-disk-usage.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/tune-for-disk-usage.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/tune-for-disk-usage.html" rel="nofollow" target="_blank">../en/tune-for-disk-usage.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch Guide [7.7]</a></span>
»
<span class="breadcrumb-link"><a href="how-to.html">How To</a></span>
»
<span class="breadcrumb-node">Tune for disk usage</span>
</div>
<div class="navheader">
<span class="prev">
<a href="faster-filtering-with-constant-keyword.html">« Use </a><a class="xref" href="constant-keyword.html" title="Constant keyword datatype"><code class="literal">constant_keyword</code></a> to speed up filtering
</span>
<span class="next">
<a href="avoid-oversharding.html">Avoid oversharding »</a>
</span>
</div>
<div class="chapter">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="tune-for-disk-usage"></a>Tune for disk usage<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h2>
</div></div></div>
<h3>
<a id="_disable_the_features_you_do_not_need"></a>Disable the features you do not need<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>By default Elasticsearch indexes and adds doc values to most fields so that they
can be searched and aggregated out of the box. For instance if you have a numeric
field called <code class="literal">foo</code> that you need to run histograms on but that you never need to
filter on, you can safely disable indexing on this field in your
<a class="xref" href="indices-create-index.html#mappings" title="Mappings">mappings</a>:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT index
{
  "mappings": {
    "properties": {
      "foo": {
        "type": "integer",
        "index": false
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1325.console"></div>
<p><a class="xref" href="text.html" title="Text datatype"><code class="literal">text</code></a> fields store normalization factors in the index in order to be
able to score documents. If you only need matching capabilities on a <code class="literal">text</code>
field but do not care about the produced scores, you can configure Elasticsearch
to not write norms to the index:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT index
{
  "mappings": {
    "properties": {
      "foo": {
        "type": "text",
        "norms": false
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1326.console"></div>
<p><a class="xref" href="text.html" title="Text datatype"><code class="literal">text</code></a> fields also store frequencies and positions in the index by
default. Frequencies are used to compute scores and positions are used to run
phrase queries. If you do not need to run phrase queries, you can tell
Elasticsearch to not index positions:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT index
{
  "mappings": {
    "properties": {
      "foo": {
        "type": "text",
        "index_options": "freqs"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1327.console"></div>
<p>Furthermore if you do not care about scoring either, you can configure
Elasticsearch to just index matching documents for every term. You will
still be able to search on this field, but phrase queries will raise errors
and scoring will assume that terms appear only once in every document.</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT index
{
  "mappings": {
    "properties": {
      "foo": {
        "type": "text",
        "norms": false,
        "index_options": "freqs"
      }
    }
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1328.console"></div>
<h3>
<a id="default-dynamic-string-mapping"></a>Don’t use default dynamic string mappings<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>The default <a class="xref" href="dynamic-mapping.html" title="Dynamic Mapping">dynamic string mappings</a> will index string fields
both as <a class="xref" href="text.html" title="Text datatype"><code class="literal">text</code></a> and <a class="xref" href="keyword.html" title="Keyword datatype"><code class="literal">keyword</code></a>. This is wasteful if you only
need one of them. Typically an <code class="literal">id</code> field will only need to be indexed as a
<code class="literal">keyword</code> while a <code class="literal">body</code> field will only need to be indexed as a <code class="literal">text</code> field.</p>
<p>This can be disabled by either configuring explicit mappings on string fields
or setting up dynamic templates that will map string fields as either <code class="literal">text</code>
or <code class="literal">keyword</code>.</p>
<p>For instance, here is a template that can be used in order to only map string
fields as <code class="literal">keyword</code>:</p>
<div class="pre_wrapper lang-console">
<pre class="programlisting prettyprint lang-console">PUT index
{
  "mappings": {
    "dynamic_templates": [
      {
        "strings": {
          "match_mapping_type": "string",
          "mapping": {
            "type": "keyword"
          }
        }
      }
    ]
  }
}</pre>
</div>
<div class="console_widget" data-snippet="snippets/1329.console"></div>
<h3>
<a id="_watch_your_shard_size"></a>Watch your shard size<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>Larger shards are going to be more efficient at storing data. To increase the size of your shards, you can decrease the number of primary shards in an index by <a class="xref" href="indices-create-index.html" title="Create index API">creating indices</a> with fewer primary shards, creating fewer indices (e.g. by leveraging the <a class="xref" href="indices-rollover-index.html" title="Rollover index API">Rollover API</a>), or modifying an existing index using the <a class="xref" href="indices-shrink-index.html" title="Shrink index API">Shrink API</a>.</p>
<p>Keep in mind that large shard sizes come with drawbacks, such as long full recovery times.</p>
<h3>
<a id="disable-source"></a>Disable <code class="literal">_source</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>The <a class="xref" href="mapping-source-field.html" title="_source field"><code class="literal">_source</code></a> field stores the original JSON body of the document. If you don’t need access to it you can disable it. However, APIs that needs access to <code class="literal">_source</code> such as update and reindex won’t work.</p>
<h3>
<a id="best-compression"></a>Use <code class="literal">best_compression</code><a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>The <code class="literal">_source</code> and stored fields can easily take a non negligible amount of disk
space. They can be compressed more aggressively by using the <code class="literal">best_compression</code>
<a class="xref" href="index-modules.html#index-codec">codec</a>.</p>
<h3>
<a id="_force_merge"></a>Force Merge<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>Indices in Elasticsearch are stored in one or more shards. Each shard is a Lucene index and made up of one or more segments - the actual files on disk. Larger segments are more efficient for storing data.</p>
<p>The <a class="xref" href="indices-forcemerge.html" title="Force merge API"><code class="literal">_forcemerge</code> API</a> can be used to reduce the number of segments per shard. In many cases, the number of segments can be reduced to one per shard by setting <code class="literal">max_num_segments=1</code>.</p>
<h3>
<a id="_shrink_index"></a>Shrink Index<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>The <a class="xref" href="indices-shrink-index.html" title="Shrink index API">Shrink API</a> allows you to reduce the number of shards in an index. Together with the Force Merge API above, this can significantly reduce the number of shards and segments of an index.</p>
<h3>
<a id="_use_the_smallest_numeric_type_that_is_sufficient"></a>Use the smallest numeric type that is sufficient<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>The type that you pick for <a class="xref" href="number.html" title="Numeric datatypes">numeric data</a> can have a significant impact
on disk usage. In particular, integers should be stored using an integer type
(<code class="literal">byte</code>, <code class="literal">short</code>, <code class="literal">integer</code> or <code class="literal">long</code>) and floating points should either be
stored in a <code class="literal">scaled_float</code> if appropriate or in the smallest type that fits the
use-case: using <code class="literal">float</code> over <code class="literal">double</code>, or <code class="literal">half_float</code> over <code class="literal">float</code> will help
save storage.</p>
<h3>
<a id="_use_index_sorting_to_colocate_similar_documents"></a>Use index sorting to colocate similar documents<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>When Elasticsearch stores <code class="literal">_source</code>, it compresses multiple documents at once
in order to improve the overall compression ratio. For instance it is very
common that documents share the same field names, and quite common that they
share some field values, especially on fields that have a low cardinality or
a <a href="https://en.wikipedia.org/wiki/Zipf%27s_law" class="ulink" target="_top">zipfian</a> distribution.</p>
<p>By default documents are compressed together in the order that they are added
to the index. If you enabled <a class="xref" href="index-modules-index-sorting.html" title="Index Sorting">index sorting</a>
then instead they are compressed in sorted order. Sorting documents with similar
structure, fields, and values together should improve the compression ratio.</p>
<h3>
<a id="_put_fields_in_the_same_order_in_documents"></a>Put fields in the same order in documents<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elastic/elasticsearch/edit/7.7/docs/reference/how-to/disk-usage.asciidoc">edit</a>
</h3>
<p>Due to the fact that multiple documents are compressed together into blocks,
it is more likely to find longer duplicate strings in those <code class="literal">_source</code> documents
if fields always occur in the same order.</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="faster-filtering-with-constant-keyword.html">« Use </a><a class="xref" href="constant-keyword.html" title="Constant keyword datatype"><code class="literal">constant_keyword</code></a> to speed up filtering
</span>
<span class="next">
<a href="avoid-oversharding.html">Avoid oversharding »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>